﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WS1.Filets;
using WS1.Models;
using WS1.Services;

namespace WS1.Controllers
{
    [BasicAuthentication("staff", "BCLyon2024")]

    public class CustomersController : ApiController
    {
        public AppResult<IEnumerable<Customers>> Get()
        {
            var result = new AppResult<IEnumerable<Customers>>();
            using (var service = new CustomersServices())
            {
                var products = service.GetAll();
                result.Data = products;
            }
            return result;
        }

        // GET: api/Products/5
        public AppResult<Customers> Get(int id)
        {
            var result = new AppResult<Customers>();

            using (var service = new CustomersServices())
            {
                var product = service.Get(id);
                result.Data = product;
            }
            return result;
        }

        public AppResult Post([FromBody] Customers product)
        {
            var result = new AppResult();
            try
            {
                using (var service = new CustomersServices())
                {
                    service.Add(product);
                }
            }
            catch (System.Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;
        }

        public AppResult Put(int id, [FromBody] Customers product)
        {
            var result = new AppResult();
            try
            {
                using (var service = new CustomersServices())
                {
                    service.Update(id, product);
                }
            }
            catch (System.Exception ex)
            {
                result.IsSuccess = false;
                result.Message = ex.Message;
            }
            return result;

        }

    }
}
